﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace SilverConversion
{
    public partial class frmMain : Form
    {
        public string _errInfo = string.Empty;
        public float _us2jtRate = 6.4F;
        public float _jt2usRate = 1 / 6.4F;
        public float _tt2usRate = 1 / 201.839F;
        public float _us2ttRate = 201.839F;

        public string _ttPriceInterface = "http://www.tjpme.com/mytest.php?rand=789";
        public string _jtPriceInterface = "http://livery.kiskey.com:8088/ajaxhandler/dataupdate.ashx";

        Common.LowPriceAlertLevel _lpal = Common.LowPriceAlertLevel.L1;
        Common.HighPriceAlertLevel _hpal = Common.HighPriceAlertLevel.H1;

        System.Timers.Timer timer2 = new System.Timers.Timer();
        
        public frmMain()
        {
            InitializeComponent();
            this.cbAlertType.SelectedIndex = 0;

            timer2.Enabled = true;
            timer2.Interval = 5000;
            timer2.Elapsed+=new System.Timers.ElapsedEventHandler(timer2_Elapsed);
        }

        public void converse(TextBox tb1, TextBox tb2, float rate)
        {
            try
            {
                if (!string.IsNullOrEmpty(tb1.Text.Trim()))
                {
                    float p1 = 0F;
                    if (float.TryParse(tb1.Text.Trim(), out p1))
                    {
                        tb2.Text = (p1 * rate).ToString();
                    }
                    else
                    {
                        MessageBox.Show("错误的数字格式");
                        tb1.Focus();
                        tb1.Select();
                        return;
                    }
                }
            }                
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region us2jt
        private void txtUS01_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS01, txtJT01, _us2jtRate);
            converse(txtUS01, txtTT01, _us2ttRate);
        }

        private void txtUS02_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS02, txtJT02, _us2jtRate);
            converse(txtUS02, txtTT02, _us2ttRate);
        }

        private void txtUS03_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS03, txtJT03, _us2jtRate);
            //converse(txtUS03, txtTT03, _us2ttRate);
        }

        private void txtUS04_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS04, txtJT04, _us2jtRate);
            //converse(txtUS04, txtTT04, _us2ttRate);
        }

        private void txtUS05_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS05, txtJT05, _us2jtRate);
            converse(txtUS05, txtTT05, _us2ttRate);
        }

        private void txtUS06_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS06, txtJT06, _us2jtRate);
            converse(txtUS06, txtTT06, _us2ttRate);
        }

        private void txtUS07_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS07, txtJT07, _us2jtRate);
            //converse(txtUS07, txtTT07, _us2ttRate);
        }

        private void txtUS08_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS08, txtJT08, _us2jtRate);
            //converse(txtUS08, txtTT08, _us2ttRate);
        }

        private void txtUS09_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS09, txtJT09, _us2jtRate);
            converse(txtUS09, txtTT09, _us2ttRate);
        }

        private void txtUS10_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS10, txtJT10, _us2jtRate);
            converse(txtUS10, txtTT10, _us2ttRate);
        }

        private void txtUS11_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS11, txtJT11, _us2jtRate);
            converse(txtUS11, txtTT11, _us2ttRate);
        }

        private void txtUS12_TextChanged(object sender, EventArgs e)
        {
            converse(txtUS12, txtJT12, _us2jtRate);
            converse(txtUS12, txtTT12, _us2ttRate);
        }

        private void txtUS13_TextChanged(object sender, EventArgs e)
        {
            //converse(txtUS13, txtJT13, _us2jtRate);
            converse(txtUS13, txtTT13, _us2ttRate);
        }

        private void txtUS14_TextChanged(object sender, EventArgs e)
        {
            //converse(txtUS14, txtJT14, _us2jtRate);
            converse(txtUS14, txtTT14, _us2ttRate);
        }
        #endregion

        #region tt2us
        private void txtTT03_TextChanged(object sender, EventArgs e)
        {
            converse(txtTT03, txtUS03, _tt2usRate);
        }

        private void txtTT04_TextChanged(object sender, EventArgs e)
        {
            converse(txtTT04, txtUS04, _tt2usRate);
        }

        private void txtTT07_TextChanged(object sender, EventArgs e)
        {
            converse(txtTT07, txtUS07, _tt2usRate);
        }

        private void txtTT08_TextChanged(object sender, EventArgs e)
        {
            converse(txtTT08, txtUS08, _tt2usRate);
        }
        #endregion


        private void frmMain_Load(object sender, EventArgs e)
        {
            UpdatePrice();
            Thread _thread = new Thread(new ThreadStart(InvokeUpdate));
            _thread.IsBackground = true;
            _thread.Start();
        }

        private void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //UpdatePrice();
        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            //UpdatePrice();
        }

        private void InvokeUpdate()
        {
            while (true)
            {
                Thread.Sleep(10000);
                if (InvokeRequired)
                {
                    this.Invoke(new UpdateP(UpdatePrice), null);
                }
                else
                {
                    MessageBox.Show("else");
                }
            }
        }

        private delegate void UpdateP();
        public void UpdatePrice()
        {
            //MessageBox.Show(DateTime.Now.ToString());
            string price_jt = Common.GetPrice(Common.GoodsType.juntai, System.Text.Encoding.Default, out _errInfo);
            string price_tt = Common.GetPrice(Common.GoodsType.tiantong, System.Text.Encoding.UTF8, out _errInfo);
            string price_us = Common.GetPrice(Common.GoodsType.us, System.Text.Encoding.Default, out _errInfo);
            if (string.IsNullOrEmpty(_errInfo))
            {
                if (price_jt.Split('|').Length >= 3)
                {
                    lblJTCurrent.Text = price_jt.Split('|')[0];
                    lblJTHighest.Text = price_jt.Split('|')[1];
                    lblJTLowest.Text = price_jt.Split('|')[2];
                }
                if (price_tt.Split('|').Length >= 3)
                {
                    lblTTCurrent.Text = price_tt.Split('|')[0];
                    lblTTHighest.Text = price_tt.Split('|')[1];
                    lblTTLowest.Text = price_tt.Split('|')[2];
                }
                if (price_us.Split('|').Length >= 3)
                {
                    lblUSCurrent.Text = price_us.Split('|')[0];
                    lblUSHighest.Text = price_us.Split('|')[1];
                    lblUSLowest.Text = price_us.Split('|')[2];
                }
                //到价提醒
                if (cbAlertType.SelectedIndex >= 0 && !string.IsNullOrEmpty(cbAlertType.SelectedItem.ToString()))
                {
                    if (cbAlertType.SelectedItem.ToString() == "君泰银")
                    {
                        if (price_jt.Split('|').Length >= 3)
                        {
                            PriceAlertGT(price_jt.Split('|')[0], txtAlertPriceH1.Text, 1);
                            PriceAlertGT(price_jt.Split('|')[0], txtAlertPriceH2.Text, 2);
                            PriceAlertLT(price_jt.Split('|')[0], txtAlertPriceL1.Text, Common.LowPriceAlertLevel.L1, 1);
                            PriceAlertLT(price_jt.Split('|')[0], txtAlertPriceL2.Text, Common.LowPriceAlertLevel.L2, 2);
                        }
                    }
                    if (cbAlertType.SelectedItem.ToString() == "天通银")
                    {
                        if (price_tt.Split('|').Length >= 3)
                        {
                            PriceAlertGT(price_tt.Split('|')[0], txtAlertPriceH1.Text, 1);
                            PriceAlertGT(price_tt.Split('|')[0], txtAlertPriceH2.Text, 2);
                            PriceAlertLT(price_tt.Split('|')[0], txtAlertPriceL1.Text, Common.LowPriceAlertLevel.L1, 1);
                            PriceAlertLT(price_tt.Split('|')[0], txtAlertPriceL2.Text, Common.LowPriceAlertLevel.L2, 2);
                        }
                    }
                    if (cbAlertType.SelectedItem.ToString() == "美元价")
                    {
                        if (price_us.Split('|').Length >= 3)
                        {
                            PriceAlertGT(price_us.Split('|')[0], txtAlertPriceH1.Text, 1);
                            PriceAlertGT(price_us.Split('|')[0], txtAlertPriceH2.Text, 2);
                            PriceAlertLT(price_us.Split('|')[0], txtAlertPriceL1.Text, Common.LowPriceAlertLevel.L1, 1);
                            PriceAlertLT(price_us.Split('|')[0], txtAlertPriceL2.Text, Common.LowPriceAlertLevel.L2, 2);
                        }
                    }
                }
                lblTime.Text = "Last updated at " + DateTime.Now.ToLongTimeString() + "";
            }
            else
            {
                MessageBox.Show("Error: " + _errInfo);
            }
        }

        private void txtJT13_TextChanged(object sender, EventArgs e)
        {
            converse(txtJT13, txtUS13, _jt2usRate);
        }

        private void txtJT14_TextChanged(object sender, EventArgs e)
        {
            converse(txtJT14, txtUS14, _jt2usRate);
        }

        public void PriceAlertGT(string price_current, string price_alert, int level)
        {
            if (price_alert.Trim() != string.Empty && (float.Parse(price_current.Trim()) >= float.Parse(price_alert)))
            {

                string tipText = "现价：" + price_current + "，已达到第" + level + "提醒高位：" + price_alert.Trim();
                if (ckbSimple.Checked)
                    tipText = price_current + " > " + price_alert.Trim();
                this.notifyIcon1.Visible = true;
                this.notifyIcon1.ShowBalloonTip(30, GetNotifyTipTitle(cbAlertType, "H")
                    , tipText, ToolTipIcon.Info);
                //MessageBox.Show("已经达到提醒价高位：" + price_alert.Trim());
            }
        }

        public void PriceAlertLT(string price_current, string price_alert, Common.LowPriceAlertLevel pal, int level)
        {
            if (price_alert.Trim() != string.Empty && (float.Parse(price_current.Trim()) <= float.Parse(price_alert)))
            {
                string tipText = "现价：" + price_current + "，已达到第" + level + "提醒低位：" + price_alert.Trim();
                if (ckbSimple.Checked)
                    tipText = price_current + " < " + price_alert.Trim();

                this.notifyIcon1.Visible = true;
                this.notifyIcon1.ShowBalloonTip(30, GetNotifyTipTitle(cbAlertType, "L")
                    , tipText, ToolTipIcon.Info);

                if ((int)_lpal < 3)
                    _lpal = _lpal + 1;
            }
        }

        public string GetNotifyTipTitle(ComboBox box, string levelType)
        {
            string title = "";                
            if (!ckbSimple.Checked)
            {
                title = "低位提醒";
                if (levelType.ToUpper() == "H")
                    title = "高位提醒";
                return box.SelectedItem.ToString() + title;
            }
            else
            {
                title = "";
                if (levelType.ToUpper() == "H")
                    title = "";
                return title;
            }
        }

        private void notifyIcon1_MouseUp(object sender, MouseEventArgs e)
        {
            notifyIcon1.Text = lblJTCurrent.Text;
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
        {

        }

        private void notifyIcon1_BalloonTipClosed(object sender, EventArgs e)
        {

        }

        private void notifyIcon1_BalloonTipShown(object sender, EventArgs e)
        {

        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            MouseEventArgs me = (MouseEventArgs)e;
            if (me.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.Show();
                    this.WindowState = FormWindowState.Normal;
                }
                else
                {
                    this.WindowState = FormWindowState.Minimized;
                }
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
        }

        private void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
        {

        }
        
        private void ContextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text.IndexOf("退出") > -1)
            {
                this.Close();
                this.Dispose();
                Application.Exit();
            }
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void frmMain_SizeChanged(object sender, EventArgs e)
        {
            //this.Visible = true;
            //this.ShowInTaskbar = true;

            if (this.WindowState == FormWindowState.Minimized)
            {
                //this.Visible = false;
                this.ShowInTaskbar = false;
            }
        }

        private void frmMain_Deactivate(object sender, EventArgs e)
        {
            //this.Hide();
        }

    }
}
